package com.tenton.servlet;

import com.tenton.pojo.ShoppingCart;
import com.tenton.service.CartService;
import com.tenton.service.Impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Date: 2021/1/20
 * @Author: Tenton
 * @Description:
 */
@WebServlet(name = "ConfirmOrderServlet",urlPatterns = "/confirmOrderServlet")
public class ConfirmOrderServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //从session中获取userId并转换为整形
        HttpSession session = request.getSession();
        int userId = (int) session.getAttribute("userId");
        CartService cartService = new CartServiceImpl();
        List<ShoppingCart> orders = cartService.getCart(userId);
        //把获取到的购物车信息链表存入request域中，传递给前端
        request.setAttribute("orders",orders);
        //请求跳转到cart页面
        request.getRequestDispatcher("/orderSure.jsp").forward(request,response);

    }
}
